package top.rainf.service.impl;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.rainf.domain.vo.Post;
import top.rainf.domain.vo.PostReply;
import top.rainf.mapper.NoticeMapper;
import top.rainf.mapper.PostMapper;
import top.rainf.mapper.PostReplyMapper;
import top.rainf.service.IPostReplyService;

import java.util.List;

/**
 * @author 星环
 * @date Wed May 10 21:04:41 CST 2023
 * @description PostReply服务的实现类
 */
@Service
public class PostReplyService implements IPostReplyService {

    @Autowired
    PostReplyMapper postReplyMapper;
    @Autowired
    PostMapper postMapper;
    @Autowired
    NoticeMapper noticeMapper;

    @Override
    public List<PostReply> getPostReplyList() {
        return postReplyMapper.selectAll();
    }

    @Override
    public int getReplyCountInPost(int postId) {
        return postReplyMapper.selectCountByPostId(postId);
    }

    @Override
    public PostReply getOneById(int id) {
        return postReplyMapper.selectById(id);
    }

    @Override
    public boolean deleteOneById(int id) {
        return postReplyMapper.deleteById(id) == 1;
    }

    @Override
    public boolean deleteAll(List<Integer> idList) {
        return postReplyMapper.deleteByIds(idList) == idList.size();
    }

    @Override
    public boolean updateById(PostReply postReply) {
        return postReplyMapper.updateById(postReply) == 1;
    }

    @Override
    public List<PostReply> getReplyListByPostIdPaged(int postId, int curPage, int numPerPage) {
        int start = (curPage - 1) * numPerPage;
        return postReplyMapper.selectByPostIdPaged(postId,start,numPerPage);
    }

    @Override
    public int addOne(PostReply postReply) {
        postReplyMapper.insertOne(postReply);
        Post post = postMapper.selectById(postReply.getPostId());
        int authorId = post.getAuthorId();
        return noticeMapper.insertOne(authorId,"您的帖子“" + post.getTitle() + "“有一条新回复！");

    }
}
